﻿using MediatR;
using Sale.Api.Application.Command.Contract;
using Sale.Domain;
using Sale.ErrorCode;
using Sale.Infrastructure.Implement;

namespace Sale.Api.Application.Handler.Contract
{
    public class DelContractHandler : IRequestHandler<DelContractCommand, ApiResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<DelContractHandler> logger;

        public DelContractHandler(DatabaseContext db,ILogger<DelContractHandler> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        public async Task<ApiResult<int>> Handle(DelContractCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var contract = db.SqlSugar.Queryable<ContractModel>().Where(x=>x.ContractId==request.ContractId).First();
            contract.IsDel = true;
            int r = await db.SqlSugar.Updateable(contract).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "销售合同删除成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "销售合同删除失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
